package com.guoxin.concurrentprogramming.chapter3.synccontrol;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class TimeLock extends Thread {

    private static final ReentrantLock LOCK = new ReentrantLock();

    @Override
    public void run() {
        try {
            if (LOCK.tryLock(5, TimeUnit.SECONDS)) {
                Thread.sleep(10000);
            } else {
                System.out.println(Thread.currentThread().getName() + " get lock filed");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (LOCK.isHeldByCurrentThread()) {
                LOCK.unlock();
            }
        }
    }

    public static void main(String[] args) {
        TimeLock tl = new TimeLock();

        new Thread(tl, "T1").start();
        new Thread(tl, "T2").start();

    }
}
